﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
using ViCode_LeVi.Configs;
using System.Windows.Media;

namespace ViCode_LeVi.Controls
{
    public class VMRootControl : Border
    {
        public VMRootControl()
            : base()
        {
            this.BorderThickness = new Thickness(0);
            this.Margin = new Thickness(10, 20, 10, 10);
            //this.BorderBrush = System.Windows.Media.Brushes.WhiteSmoke;
        }
    }
    public class VMTextBlock_Trade_1 : VMTextBlock
    {
        public VMTextBlock_Trade_1()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }

        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlock_SpaceLine : VMTextBlock
    {
        public VMTextBlock_SpaceLine()
        {
            this.Visibility = Visibility.Hidden;
            this.Text = "Hidden";
        }
    }
    public class VMTextBlock_Fincance_Title : VMTextBlockExt
    {
        public VMTextBlock_Fincance_Title()
            : base()
        {
            this.TextWrapping = TextWrapping.WrapWithOverflow;
            this.FontSize = 15;
            this.FontWeight = FontWeights.Bold;
            Foreground = new SolidColorBrush(Color.FromRgb(0xB3, 0x00, 0x1A));
        }

    }
    public class VMTextBlock_Fincance_Space_Header : VMTextBlockExt
    {
        public VMTextBlock_Fincance_Space_Header()
            : base()
        {
            this.Visibility = Visibility.Hidden;
            this.Text = "Hidden";
            if (this.FontSize > 0)
                this.FontSize = this.FontSize * 2;
        }
    }
    public class VMTextBlock_Trade_1_CanEdit : VMTextBlockExt
    {
        public VMTextBlock_Trade_1_CanEdit()
            : base()
        {
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }

    }
    public class VMTextBlock_Trade_SignEqual : VMTextBlock
    {
        public VMTextBlock_Trade_SignEqual()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.Text = "=";
            this.Margin = new Thickness(2, 2, 2, 20);
        }
        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlock_Trade_2 : VMTextBlock
    {
        public VMTextBlock_Trade_2()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }

        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlock_Trade_2_CanEdit : VMTextBlockExt
    {
        public VMTextBlock_Trade_2_CanEdit()
            : base()
        {
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }

    }
    public class VMTextBlock_Price_GroupHeader : VMTextBlock
    {
        public VMTextBlock_Price_GroupHeader()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.TextWrapping = TextWrapping.WrapWithOverflow;
            this.FontWeight = FontWeights.Bold;
        }

        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlock_Price_GroupHeader_CanEdit : VMTextBlockExt
    {
        public VMTextBlock_Price_GroupHeader_CanEdit()
            : base()
        {
            this.TextWrapping = TextWrapping.WrapWithOverflow;
            this.FontWeight = FontWeights.Bold;
        }

    }
    public class VMTextBlock_Price_Total : VMTextBlock
    {
        public VMTextBlock_Price_Total()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.TextWrapping = TextWrapping.WrapWithOverflow;
            this.FontWeight = FontWeights.Bold;
        }

        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlock_Price_Total_CanEdit : VMTextBlockExt
    {
        public VMTextBlock_Price_Total_CanEdit()
            : base()
        {
            this.TextWrapping = TextWrapping.WrapWithOverflow;
            this.FontWeight = FontWeights.Bold;
        }
    }
    public class VMTextBlock_Price_1 : VMTextBlock
    {
        public VMTextBlock_Price_1()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }

        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlock_Price_1_CanEdit : VMTextBlockExt
    {
        public VMTextBlock_Price_1_CanEdit()
            : base()
        {
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }
    }
    public class VMTextBlock_Price_2 : VMTextBlock
    {
        public VMTextBlock_Price_2()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }

        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlockMenu : TextBlock
    {
        public VMTextBlockMenu()
            : base()
        {
            if (Style == null)
                Style = Application.Current.Resources[typeof(TextBlock)] as Style;
            this.FontSize = 12;
        }

    }
    public class VMTextBlockStep : TextBlock
    {
        public VMTextBlockStep()
            : base()
        {
            if (Style == null)
                Style = Application.Current.Resources[typeof(TextBlock)] as Style;
            this.FontSize = 12;
        }

    }
    public class VMTextBlockNoWrap : TextBlock
    {
        public VMTextBlockNoWrap()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            this.TextWrapping = TextWrapping.NoWrap;
        }
        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
        }
    }
    public class VMTextBlock : TextBlock
    {
        public VMTextBlock()
            : base()
        {
            if (Style == null)
                Style = GetStyle();
            //try
            //{
            //    this.FontFamily = new System.Windows.Media.FontFamily(VMConfig.Intance.UIFont.TextBlock_FontName);
            //    this.FontSize = VMConfig.Intance.UIFont.TextBlock_FontSize;
            //}
            //catch { }
            this.TextWrapping = TextWrapping.WrapWithOverflow;
        }

        private static Style GetStyle()
        {
            if (Application.Current == null)
                return null;
            var lStyle = Application.Current.Resources[typeof(TextBlock)] as Style;
            return lStyle;
            //var lNewStyle = new Style() { BasedOn = lStyle, TargetType = typeof(TextBlock) };
            //if (lStyle != null)
            //{
            //    Setter sFontFamily = null;
            //    Setter sFontSize = null;
            //    foreach (var s in lStyle.Setters)
            //    {
            //        Setter set = s as Setter;
            //        if (set != null)
            //        {
            //            Setter lsFontFamily = null;
            //            Setter lsFontSize = null;
            //            if (set.Property == TextBlock.FontFamilyProperty)
            //            {
            //                sFontFamily = sFontFamily = set;
            //            } if (set.Property == TextBlock.FontSizeProperty)
            //            {
            //                sFontSize = sFontSize = set;
            //            }
            //            if (sFontFamily == null || sFontSize == null)
            //                lNewStyle.Setters.Add(set);
            //        }
            //    }
            //    //
            //    if (sFontFamily == null)
            //        sFontFamily = new Setter(TextBlock.FontFamilyProperty, new System.Windows.Media.FontFamily(VMConfig.Intance.UIFont.TextBlock_FontName));
            //    lNewStyle.Setters.Add(sFontFamily);
            //    if (sFontSize == null)
            //        sFontSize = new Setter(TextBlock.FontWeightProperty, VMConfig.Intance.UIFont.TextBlock_FontSize);
            //    lNewStyle.Setters.Add(sFontSize);
            //}
            //Application.Current.Resources[typeof(TextBlock)] = lNewStyle;
            //return lNewStyle;
        }
    }
    public class VMTextBlockExt_Header : VMTextBlockExt
    {
        public VMTextBlockExt_Header()
            : base(12)
        {
            Foreground = new SolidColorBrush(Color.FromRgb(0xB3, 0x00, 0x1A));
        }
    }
    public class VMTextBlockExt_Finance_Investment_GroupHeader : VMTextBlockExt
    {
        public VMTextBlockExt_Finance_Investment_GroupHeader()
        {
            this.FontWeight = FontWeights.Bold;
            this.Margin = new Thickness(2, 10, 2, 2);
        }
    }
    public class VMTextBlockExt_Header_TabItem : VMTextBlockExt
    {
        TabControl TabMain = null;
        TabItem TabItemMain = null;
        public VMTextBlockExt_Header_TabItem()
            : base(12)
        {
            this.Loaded += new RoutedEventHandler(VMTextBlockExt_Header_TabItem_Loaded);
        }

        void VMTextBlockExt_Header_TabItem_Loaded(object sender, RoutedEventArgs e)
        {
            SetColor();
            TabMain = DependencyHelper.GetAncestorByType(this, typeof(TabControl)) as TabControl;
            TabItemMain = DependencyHelper.GetAncestorByType(this, typeof(TabItem)) as TabItem;
            if (TabMain != null)
            {
                TabMain.SelectionChanged += new SelectionChangedEventHandler(TabMain_SelectionChanged);
            }
            //if (TabItemMain != null)
            //{
            //    var t = TabItemMain.Triggers.FirstOrDefault(c => ((Trigger)c).Property == TabItem.IsMouseOverProperty
            //        && ((Trigger)c).Value == (object)true);
            //    if (t != null)
            //    { 
            //        TabItemMain.Triggers.Remove(t);
            //    }
            //}
        }

        void TabMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SetColor();
        }

        private void SetColor()
        {
            var item = DependencyHelper.GetAncestorByType(this, typeof(TabItem)) as TabItem;
            if (item != null && item.IsSelected)
            {
                Foreground = new SolidColorBrush(Color.FromRgb(0xB3, 0x00, 0x1A));
            }
            else
            {
                Foreground = new SolidColorBrush(Color.FromRgb(0xE0, 0xDF, 0xFF));
            }
        }
    }
    public static class VMTextBlockExt_ControlTextInfoInDatas
    {
        public static List<VMTextBlockExt_ControlTextInfoInData> listItems = new List<VMTextBlockExt_ControlTextInfoInData>();
        public static void Add(VMTextBlockExt_ControlTextInfoInData item)
        {
            if (!listItems.Contains(item))
                listItems.Add(item);             
        }
        public static void RefreshUI()
        {
            foreach (var item in listItems)
            {
                item.RefreshUI();
            }
        }
    }
    public class VMTextBlockExt_ControlTextInfoInData : VMTextBlockExt
    {
        public VMTextBlockExt_ControlTextInfoInData()
            : base()
        {
            VMTextBlockExt_ControlTextInfoInDatas.Add(this);
            RefreshUI();
        }
        public void RefreshUI()
        {
            this.DataContext = ViCode_LeVi.Langs.ControlTextInfoInData.Intance;
        }
    }
    public class VMTextBlockExt : Grid
    {
        public Brush Foreground
        {
            get { return _TextBlock != null ? _TextBlock.Foreground : Brushes.Black; }
            set
            {
                if (_TextBlock != null)
                    _TextBlock.Foreground = value;
                if (_TextBox != null)
                    _TextBox.Foreground = value;
            }
        }

        public string Text
        {
            get { return _TextBlock != null ? _TextBlock.Text : string.Empty; }
            set { if (_TextBlock != null)_TextBlock.Text = value; }
        }
        double _FontSize = 0;
        public VMTextBlockExt()
            : base()
        {
            Init();
        }
        public TextWrapping TextWrapping
        {
            get { return _TextBlock.TextWrapping; }
            set { _TextBlock.TextWrapping = _TextBox.TextWrapping = value; }
        }
        public TextAlignment TextAlignment
        {
            get { return _TextBlock.TextAlignment; }
            set { _TextBlock.TextAlignment = _TextBox.TextAlignment = value; }
        }
        public double FontSize
        {
            get { return _TextBlock.FontSize; }
            set { _TextBlock.FontSize = _TextBox.FontSize = value; }
        }
        public FontWeight FontWeight
        {
            get { return _TextBlock.FontWeight; }
            set { _TextBlock.FontWeight = _TextBox.FontWeight = value; }
        }
        public VMTextBlockExt(double aFontSize)
            : base()
        {
            _FontSize = aFontSize;
            Init();
        }
        public bool Focus_Question()
        {
            if (_TextBox != null && _TextBox.Visibility == Visibility.Visible)
            {
                _TextBox.Focus();
                return true;
            }
            return false;
        }
        private void Init()
        {
            VMTextBlockExts.Add(this);
            this.Children.Add(_TextBox);
            this.Children.Add(_TextBlock);
            if (_FontSize > 0)
            {
                _TextBox.FontSize = _FontSize;
                _TextBlock.FontSize = _FontSize;
            }
            RefreshVisibility();
        }

        //public VMTextBlockExt(FontWeight fontWeight)
        //    : base()
        //{
        //    Init();
        //    _TextBlock.FontWeight = fontWeight;
        //}
        #region TextBlock or TextBox
        #region IsInDesignMode
        private static bool _IsInDesignMode = false;
        public static bool IsInDesignMode
        {
            get { return _IsInDesignMode; }
            set
            {
                _IsInDesignMode = value;
                foreach (var ctrl in VMTextBlockExts)
                {
                    ctrl.RefreshVisibility();
                }
            }
        }
        #endregion
        /// <summary>
        /// Do chương trình chỉ có mấy nhieu control nên mới dám su dung cách này
        /// </summary>
        private static List<VMTextBlockExt> VMTextBlockExts = new List<VMTextBlockExt>();

        public void RefreshVisibility()
        {
            if (VMTextBlockExt.IsInDesignMode == true)
            {
                _TextBlock.Visibility = System.Windows.Visibility.Collapsed;
                _TextBox.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                _TextBlock.Visibility = System.Windows.Visibility.Visible;
                _TextBox.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        #endregion


        VMTextBlock _TextBlock = new VMTextBlock() { TextWrapping = TextWrapping.WrapWithOverflow };
        VMTextBox_MultiLine _TextBox = new VMTextBox_MultiLine();

        public string _TextPath;
        public string TextPath
        {
            get
            {
                return _TextPath;
            }
            set
            {
                if (_TextPath != value)
                {
                    _TextPath = value;
                    _TextBlock.SetBinding(VMTextBlock.TextProperty, new Binding(value));
                    _TextBox.SetBinding(VMTextBox.TextProperty, new Binding(value));
                }
            }
        }
    }
}
